//
//  HomeViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/11.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "HomeViewController.h"
#import "BLHeader.h"
#import <Masonry.h>
#import "FirstWeChatLoginView.h"
#import "HomeCollectionViewCell.h"
#import "MineOrderViewController.h"
#import "MineViewController.h"
#import <Masonry.h>
#import "HomeHeadView.h"
#import "HomePageViewController.h"
#import "HomeNet.h"

@interface HomeViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, HomeHeadViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (strong, nonatomic) IBOutlet  UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *controllers;
@property (nonatomic, strong) HomeHeadView *head;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    self.navigationController.navigationBar.barTintColor = [UIColor groupTableViewBackgroundColor];
    _head = [HomeHeadView homeHeadView];
    _head.frame = CGRectMake(0, 0, KScreenWidth, 64);
    _head.intrinsicContentSize = CGSizeMake(KScreenWidth, 64);
    self.navigationItem.titleView = _head;
    _head.delegate = self;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.flowLayout.itemSize = CGSizeMake(KScreenWidth, KScreenHeight - 64);
    self.collectionView.pagingEnabled = YES;
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ReuseIdentify];
    self.collectionView.delegate = self;
    self.collectionView.dataSource =self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    HomePageViewController *controller = [[HomePageViewController alloc] init];
    MineOrderViewController *order = [[MineOrderViewController alloc] init];
    MineViewController *mine = [[MineViewController alloc] init];
    self.controllers = @[controller,order,mine];
    [self addChildViewController:order];
    [self addChildViewController:mine];
    [self checkTel];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ReuseIdentify forIndexPath:indexPath];
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UIViewController *controller = self.controllers[indexPath.row];
    [cell.contentView addSubview:controller.view];
    [controller.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(cell.contentView);
    }];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

- (void)scrollerToIndex:(NSInteger)index {
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}


- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    NSInteger index = targetContentOffset->x / KScreenWidth;
    [self.head changeSomeState:index];
    

}


#pragma mark 判断是否为第一次微信登录  即有没有电话号码
- (void)checkTel {
    // 如果没有电话号码 要求用户填入手机号码
    NSString *tel = [[NSUserDefaults standardUserDefaults] objectForKey:USERTEL];
    if (!tel || [tel length] == 0) {
        FirstWeChatLoginView *first = [FirstWeChatLoginView defaultFirstView];
        [first show:[UIApplication sharedApplication].delegate.window animation:YES];
    }
}

@end


